package com.jsy.mini12306.manage.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsy.mini12306.common.entity.Station;
import com.jsy.mini12306.manage.entity.StationExcel;
import com.jsy.mini12306.manage.mapper.StationMapper;
import com.jsy.mini12306.manage.service.ManageStationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;


/**
 * 车站（字典表）(Station)表服务实现类
 *
 * @author: SongyangJi
 * @since: 2021-09-03 02:21:52
 * @description:
 */

@Slf4j
@Service("manageStationService")
public class ManageStationServiceImpl extends ServiceImpl<StationMapper, Station> implements ManageStationService {
    @Resource
    private StationMapper stationMapper;

    @Override
    public Station queryByStationName(String stationName) {
        return stationMapper.selectOne(new QueryWrapper<Station>().eq("station_name",stationName));
    }

    @Override
    public void savaStationsFromExcel(InputStream inputStream) {
        ExcelReader excelReader = EasyExcel.read(inputStream, StationExcel.class, new AnalysisEventListener<StationExcel>() {
            @Override
            public void invoke(StationExcel stationExcel, AnalysisContext analysisContext) {
                stationMapper.insert(stationExcel.wrapper());
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).build();
        // 读取sheet0
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        // 将 sheet 写入 Reader
        excelReader.read(readSheet);
    }


}
